using DG.Tweening;
using System.Collections;
using UnityEngine;

/*
 * 向敌人冲击的敌人
 */
namespace Enemy
{
    public class Enemy2 : BaseEnemy
    {
/*        [SerializeField]
        private float shockSpeed; // 冲击速度*/
        [SerializeField]
        private float shockForce; // 冲击力
        [SerializeField]
        private float shockDistance = 10; // 冲击距离

        private void Start()
        {
            Hp = MaxHp;
            bulletPrefab = Resources.Load<GameObject>("Prefabs/Enemy/Bullet/EnemyBullet1");
            base.Start();
        }

        private void Update()
        {
            //Action();
            base.Update();
        }

        public override void Action()
        {

            // 面向敌人方向，只改变Y轴旋转
            if (Player.Player.instance != null)
            {
                Vector3 targetDirection = Player.Player.instance.transform.position - transform.position;
                targetDirection.y = 0; // 忽略Y轴方向

                Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
                transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, targetRotation.eulerAngles.y, transform.rotation.eulerAngles.z);
            }
            //冲向敌人
            if (Player.Player.instance != null)
            {
                if (Time.time >= nextAttackTime)
                {
                    Vector3 direction = transform.forward.normalized;
                    Vector3 pos = transform.position + direction * shockDistance;
                    //这里的force随机，为了不同的敌人落点不同，防止多个敌人重叠
                    rb.AddForce(direction * (shockForce + Random.Range(-1,1)), ForceMode.Impulse);
                    nextAttackTime = Time.time + attackCooldown;
                }
            }
        }

        /// <summary>
        /// 玩家子弹碰撞
        /// </summary>
        /// <param name="other"></param>
        public void OnTriggerEnter(Collider other)
        {
            base.OnTriggerEnter(other);
            if (other.gameObject.CompareTag("Bullet"))
            {
                GetHit();
            }

            if(other.gameObject.CompareTag("Wall"))
                rb.velocity = Vector3.zero;

        }

    }
}
